package Experiment;

/**
 * Created by user on 2017/6/8.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.Security;
import java.util.Scanner;

import static Experiment.ThreeDES.encryptMode;

public class SocketClient {
    // 搭建客户端
    public static void main(String[] args) throws IOException {
        try {
            Encode encode = new Encode();
            Scanner scanner=new Scanner(System.in);

            // 1、创建客户端Socket，指定服务器地址和端口
             Socket socket=new Socket("127.0.0.1",8080);
          //  Socket socket = new Socket("127.0.0.1", 5209);
            System.out.println("客户端启动成功");
            // 2、获取输出流，向服务器端发送信息
            // 向本机的52000端口发出客户请求
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            // 由系统标准输入设备构造BufferedReader对象
            PrintWriter write = new PrintWriter(socket.getOutputStream());
            // 由Socket对象得到输出流，并构造PrintWriter对象
            //3、获取输入流，并读取服务器端的响应信息
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 由Socket对象得到输入流，并构造相应的BufferedReader对象
            String readline,readline2;

           //readline = br.readLine(); // 从系统标准输入读入一字符串
            String encodeRules="67,-78,48,-68,-9,63,41,-42,70,-109,24,74,97,-8,71,92,123,46,-80,-123,-100,-29,-120,49,-39,55,-100,-31,62,-46,39,42,-10,-126,-88,-59,-86,-22,67,-95,-81,-122,-107,114,-31,113,-17,110,26,122,-29,57,-27,-70,-67,40,-108,14,-17,8,1,-71,-93,-1,-22,-87,52,12,-29,61,-117,126,73,-69,-62,-48,-38,-128,7,77,95,-88,-22,88,-5,-55,-99,39,107,-128,67,-120,49,-127,-82,-12,-117,13,73,105,-45,118,122,1,-124,123,-18,65,47,-40,105,-30,111,51,13,-83,22,-89,114,-62,-55,98,-15,-8,40,36,37,-88,\n";
            System.out.println("请输入要加密的内容:");
            String question = br.readLine();
            popLast pop = new popLast();
            pop.evaluate(question);
            readline2=pop.getMessage();
            String x=readline2;
            MessageDigest m=MessageDigest.getInstance("MD5");
            m.update(x.getBytes("UTF8"));
            byte s[ ]=m.digest( );
            String result="";
            for (int i=0; i<s.length; i++){
                result+=Integer.toHexString((0x000000ff & s[i]) |
                        0xffffff00).substring(6);
            }
         //   System.out.println("客户端MD5的值为" + result);
           // write.println(result);
         //   write.flush();
            String content =readline2;
            System.out.println("客户端MD5的值为" + result+"根据DH生成的密钥"+encodeRules+"加密后的密文是:"+encode.AESEncode(encodeRules, content));



            while (!content.equals("end")) {
                // 若从标准输入读入的字符串为 "end"则停止循环
                write.println(new String(encode.AESEncode(encodeRules, content)));
                write.println(result);
                // 将从系统标准输入读入的字符串输出到Server
                write.flush();
                // 刷新输出流，使Server马上收到该字符串
                System.out.println("Client:" + new String(encode.AESEncode(encodeRules, content)));
                // 在系统标准输出上打印读入的字符串
                System.out.println("Server:" + in.readLine());
                // 从Server读入一字符串，并打印到标准输出上
                readline = br.readLine(); // 从系统标准输入读入一字符串
            } // 继续循环
            //4、关闭资源
            write.close(); // 关闭Socket输出流
            in.close(); // 关闭Socket输入流
            socket.close(); // 关闭Socket
        } catch (Exception e) {
            System.out.println("can not listen to:" + e);// 出错，打印出错信息
        }
    }

}